package cn.agiledata.bank.myspace.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountSetup;
import cn.agiledata.bank.hibernateBean.AccountSetupD;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class UserSetupAction extends IPBBaseAction {

	private AccountTemplateInter at = null;
	
	public ActionForward preProcess(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		at = (AccountTemplateInter)IPBMgrFactory.getMgr("accountTemplate");
			
		long accountId = WeatherUtil.getAccountIdFromSession(request);	
		Account acco  = at.getAccount(String.valueOf(accountId));
		
		request.setAttribute("subAccounts",acco.getSubAccounts());
		DynaActionForm fill = (DynaActionForm)actionForm;
		
		String Action= request.getParameter("action");
		if (Action == null){
			Action = fill.getString("action");
		}
		String subAccountId = at.getAccount(String.valueOf(accountId)).getDefaultSubAccount().getId().toString();
		if (Action != null && Action.equals("main")) {
			if ( fill.get("subAccount") != null && !fill.get("subAccount").equals("")){
				subAccountId = (String)fill.get("subAccount");
			}
			List result = at.getSetup(subAccountId);
			fill.getMap().clear();
			pickUpPrimaryCheckBox(fill,result);
			fill.set("subAccount",subAccountId);
			return mapping.findForward("UserSetup");
		}

		List resultList = new ArrayList();
		
		Map fillMap = new HashMap();
		fillMap.put("1",fill.get("phoneInputMoney"));
		fillMap.put("2",fill.get("phoneOutputMoney"));
		fillMap.put("3",fill.get("phoneReceiveMoney"));
		fillMap.put("4",fill.get("phoneSendMoney"));
		AccountSetup as = new AccountSetup();
		as.setAccount(at.getSubAccount((String)fill.get("subAccount")));
		as.setValidFlag("Y");
		as.setSetId(Constant.ACCOUNT_SETUP_TYPE_A);//电话
		WeatherUtil.fillCreateAndUpdateInfo(as, acco.getId().toString());
		fillAccountSetup(acco, as, fillMap);
		resultList.add(as);
		
		fillMap.clear();
		fillMap.put("1",fill.get("messageInputMoney"));
		fillMap.put("2",fill.get("messageOutputMoney"));
		fillMap.put("3",fill.get("messageReceiveMoney"));
		fillMap.put("4",fill.get("messageSendMoney"));
		AccountSetup as1 = new AccountSetup();
		as1.setAccount(at.getSubAccount((String)fill.get("subAccount")));
		as1.setValidFlag("Y");
		as1.setSetId(Constant.ACCOUNT_SETUP_TYPE_B);//短信
		WeatherUtil.fillCreateAndUpdateInfo(as1, acco.getId().toString());
		fillAccountSetup(acco, as1, fillMap);
		resultList.add(as1);		
		at.saveOrUpdateSetup(resultList);
		
		return mapping.findForward("UserSetup");
	}

	private void pickUpPrimaryCheckBox(DynaActionForm fill,List result) {
		if (result.size() > 0) {
			for (int i = 0; i < result.size(); i++) {
				AccountSetup as = (AccountSetup) result.get(0);
				if (as.getSetId().equals(Constant.ACCOUNT_SETUP_TYPE_A)) {
					Iterator setUpD = as.getSetupds().iterator();
					while (setUpD.hasNext()) {
						AccountSetupD asd = (AccountSetupD) setUpD.next();
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_BANKDEPOSIT))
						// 如果是充值
						{
							fill.set("phoneInputMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_GAINMONEY)) {
							fill.set("phoneOutputMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_GATHERING)) {
							fill.set("phoneReceiveMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_PAYMONEY)) {
							fill.set("phoneSendMoney", "Y");
						}
					}
				}
				if (as.getSetId().equals(Constant.ACCOUNT_SETUP_TYPE_B)) {
					Iterator setUpD = as.getSetupds().iterator();
					while (setUpD.hasNext()) {
						AccountSetupD asd = (AccountSetupD) setUpD.next();
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_BANKDEPOSIT))
						// 如果是充值
						{
							fill.set("messageInputMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_GAINMONEY)) {
							fill.set("messageOutputMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_GATHERING)) {
							fill.set("messageReceiveMoney", "Y");
						}
						if (asd.getActionId().equals(
								Constant.MODEL_TYPE_PAYMONEY)) {
							fill.set("messageSendMoney", "Y");
						}
					}
				}
			}
		}
	}

	private void fillAccountSetup(Account acco, AccountSetup as
			,Map fill) {
		Map fill1 = fill;
		as.getSetupds().clear();
		if (fill1.get("1").equals("Y") ) {
			AccountSetupD asd = new AccountSetupD();
			asd.setActionId(Constant.MODEL_TYPE_BANKDEPOSIT);
			asd.setActionName("");
			asd.setSetSeq(as);
			WeatherUtil.fillCreateAndUpdateInfo(asd, acco.getId().toString());
			as.getSetupds().add(asd);
		}
		if (fill1.get("2").equals("Y") ) {
			AccountSetupD asd = new AccountSetupD();
			asd.setActionId(Constant.MODEL_TYPE_GAINMONEY);
			asd.setActionName("");
			asd.setSetSeq(as);
			WeatherUtil.fillCreateAndUpdateInfo(asd, acco.getId().toString());
			as.getSetupds().add(asd);
		}
		if (fill1.get("3").equals("Y") ) {
			AccountSetupD asd = new AccountSetupD();
			asd.setActionId(Constant.MODEL_TYPE_GATHERING);
			asd.setActionName("");
			asd.setSetSeq(as);
			WeatherUtil.fillCreateAndUpdateInfo(asd, acco.getId().toString());
			as.getSetupds().add(asd);
		}
		if (fill1.get("4").equals("Y") ) {
			AccountSetupD asd = new AccountSetupD();
			asd.setActionId(Constant.MODEL_TYPE_PAYMONEY);
			asd.setActionName("");
			asd.setSetSeq(as);
			WeatherUtil.fillCreateAndUpdateInfo(asd, acco.getId().toString());
			as.getSetupds().add(asd);
		}
	}

}
